
// 定义类
class DataSource {
    // 以`#`为前缀的属性是私有属性
    #comments = [
        { id: 250, title: 'Ant Design Title 1', desc: '风里雨里，我在东理等你。学妹啊，快点来啊' },
        { id: 231, title: 'Ant Design Title 2', desc: '风里雨里，我在东理等你。学妹啊，快点来啊' },
        { id: 298, title: 'Ant Design Title 3', desc: '风里雨里，我在东理等你。学妹啊，快点来啊' }
    ]
    #blogPosts = [
        { id: 1234, title: 'Hello, Mr Tree' , desc: '树哥好' },
        { id: 5678, title: '新少林五组' , desc: '李连杰和谢苗携手演绎...' },
    ]
    // 定义一个用来存放监听器的数组
    #handlers = []
    getComments(){
        return this.#comments;
    }
    addComment(){
        let id = this.#comments.reduce( (p,c) => p > c.id ? p : c.id , 0);
        this.#comments.push( { id: id + 1, title: 'hello' , desc: 'MR. tree' });
        this.#handlers.forEach( h => h() );
    }
    getBlogPost(pid){
        return this.#blogPosts.find( p => p.id === pid );
    }
    setBlogPost(pid) {
        let post = this.#blogPosts.find( p => p.id === pid );
        if( post ) {
            post.title = '你好';
            post.desc = '你好，树先生';
        }
        this.#handlers.forEach( h => h() );
    }
    addChangeListener( handler ){
        console.log( 'add change listener' );
        this.#handlers.push( handler );
    }
    removeChangeListener( handler ){
        console.log( 'remove change listener' );
        let index = this.#handlers.findIndex( h => h === handler );
        index != -1 && this.#handlers.splice( index , 1 );
    }
}

// 创建实例
const dataSource = new DataSource();

// 导出实例
export default dataSource ;